/** m2settings is a GUI to configure Maven 2 settings.xml 
 * 
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 * 
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version. 
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * JAddMirror2.java
 *
 * Created on April 22, 2007, 12:22 PM
 */

package info.decamps.m2settings.swing;

import info.decamps.m2settings.beans.MyMirror;
import info.decamps.m2settings.documentation.DocumentationProvider;
import info.decamps.m2settings.model.MirrorTableModel;
import java.net.MalformedURLException;
import org.apache.log4j.Logger;

/**
 *
 * @author  regis
 */
public class JFrameMirror extends javax.swing.JFrame {
    private Logger log = Logger.getLogger(JFrameMirror.class);
    private MirrorTableModel model;
    private MyMirror myMirror;
    private int index=-1;
    DocumentationProvider documentation=DocumentationProvider.getInstance();
    
    /** Creates new form JAddMirror */
    public JFrameMirror(MirrorTableModel model) {
        this.model=model;
        this.myMirror=new MyMirror();
        log.debug("temporary new mirror created");
        initComponents();
    }
    public JFrameMirror(MirrorTableModel model, int index) {
        this.model=model;
        this.myMirror=new MyMirror(model.getRow(index));
        this.index=index;
        log.debug("edit an existing mirror #"+index);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabelId = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jLabelName = new javax.swing.JLabel();
        jLabelMirrorOf = new javax.swing.JLabel();
        jLabelUrl = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jTextFieldMirrorOf = new javax.swing.JTextField();
        jTextFieldURL = new javax.swing.JTextField();
        jButtonCancel = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jLabelError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabelId.setText("id");

        jTextFieldId.setText(myMirror.getId());

        jLabelName.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabelName.setText("name");

        jLabelMirrorOf.setText("mirror of");

        jLabelUrl.setText("URL");

        jTextFieldName.setText(myMirror.getName()
        );
        jTextFieldName.setToolTipText(documentation.getDescription("/Mirror/name"));

        jTextFieldMirrorOf.setText(myMirror.getMirrorOf());
        jTextFieldMirrorOf.setToolTipText(documentation.getDescription("/Mirror/mirrorOf"));

        jTextFieldURL.setText(myMirror.getUrl());
        jTextFieldURL.setToolTipText(documentation.getDescription("/Mirror/url"));

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jLabelError.setFont(new java.awt.Font("Dialog", 3, 12));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(40, 40, 40)
                        .add(jTextFieldId, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabelId))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabelName)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabelMirrorOf)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldMirrorOf, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabelUrl)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldURL, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jButtonCancel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 157, Short.MAX_VALUE)
                        .add(jButtonOK))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabelError)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelId)
                    .add(jTextFieldId, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(6, 6, 6)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelName)
                    .add(jTextFieldName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelMirrorOf)
                    .add(jTextFieldMirrorOf, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelUrl)
                    .add(jTextFieldURL, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabelError)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 52, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonCancel)
                    .add(jButtonOK))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        myMirror.setId(jTextFieldId.getText());
        myMirror.setName(jTextFieldName.getText());
        myMirror.setMirrorOf(jTextFieldMirrorOf.getText());
        try {
            myMirror.setUrl(jTextFieldURL.getText());
            if (index==-1) {
                log.debug("add new mirror to model");
                model.addRow(myMirror.getMirror());
            } else {
                log.debug("mirror configurationn changed");
                model.fireChange(index);
            }
            dispose();
        } catch (MalformedURLException ex) {
            log.warn(ex.getMessage());
            jLabelError.setText("Invalid URL");
        }        
    }//GEN-LAST:event_jButtonOKActionPerformed
    
    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JLabel jLabelError;
    private javax.swing.JLabel jLabelId;
    private javax.swing.JLabel jLabelMirrorOf;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelUrl;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldMirrorOf;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JTextField jTextFieldURL;
    // End of variables declaration//GEN-END:variables
    
}
